package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.UserGetNoticeListDto;
import com.example.medical.entity.pojo.Notice;
import com.example.medical.entity.pojo.NoticeType;
import com.example.medical.mapper.NoticeTypeMapper;
import com.example.medical.service.NoticeService;
import com.example.medical.mapper.NoticeMapper;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import com.example.medical.utils.config.EverythingConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2024-03-31 22:42:19
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeTypeMapper noticeTypeMapper;
    @Autowired
    private EverythingConfig everythingConfig;

    @Override
    public Result UserGetNoticeList(UserGetNoticeListDto dto) {
        dto.checkParam();
        IPage<Notice> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Notice> wrapper=new LambdaQueryWrapper<>();
        if (dto.getNoticeTypeId()!=null) {
            wrapper.eq(Notice::getNoticeTypeId,dto.getNoticeTypeId());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(Notice::getNoticeName,dto.getName());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result AdminRemoveNoticeArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id : ids) {
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminUpdateNotice(Long id, String name, Long typeId, MultipartFile file, String message) throws Exception {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Notice checkNotice = getById(id);
        if (checkNotice==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        if (StringUtils.isNotBlank(name)) {
            checkNotice.setNoticeName(name);
        }
        if (typeId!=null) {
            NoticeType checkNoticeType = noticeTypeMapper.selectById(typeId);
            checkNotice.setNoticeTypeId(checkNoticeType.getNoticeTypeId());
            checkNotice.setNoticeTypeName(checkNoticeType.getNoticeTypeName());
        }
        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
            String url = everythingConfig.Upload(file);
            checkNotice.setNoticeImg(url);
        }
        if (StringUtils.isNotBlank(message)) {
            checkNotice.setNoticeMessage(message);
        }
        boolean checkUpdate = updateById(checkNotice);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminSaveNotice(String name, Long typeId, MultipartFile file, String message) throws Exception {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        NoticeType checkNoticeType = noticeTypeMapper.selectById(typeId);
        if (checkNoticeType==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        String url = everythingConfig.Upload(file);
        Notice notice=new Notice();
        notice= notice.builder()
                .noticeName(name)
                .noticeTypeId(checkNoticeType.getNoticeTypeId())
                .noticeTypeName(checkNoticeType.getNoticeTypeName())
                .noticeImg(url)
                .noticeMessage(message)
                .saveTime(String.valueOf(LocalDateTime.now())).build();
        boolean checkSave = save(notice);
        if (!checkSave) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }
}




